package com.terrynoya.fight.commands
{
	import com.terrynoya.fight.emuns.CommandButton;
	import com.terrynoya.fight.emuns.CommandDirection;
	import com.terrynoya.fight.emuns.PlayerButton;

	public class CommandElement
	{
		private var _direction:int;
		private var _button:int;
		private var _triggerTime:int;
		private var _nothingElse:Boolean;
		
		private var _heldDown:Boolean;
		
		private var _matchHash1:int;
		private var _matchHash2:int;
		
		public function CommandElement(direction:int,button:int,triggerTime:int,heldDown:Boolean,nothingElse:Boolean)
		{
			this._heldDown = heldDown;
			this._direction = direction;
			this._button = button;
			this._triggerTime = triggerTime;
			this._nothingElse = nothingElse;
			this._matchHash1 = this.buildHash1();
			this._matchHash2 = this.buildHash2();
		}

		public function get button():int
		{
			return _button;
		}

		public function set button(value:int):void
		{
			_button = value;
		}

		public function get direction():int
		{
			return _direction;
		}

		public function set direction(value:int):void
		{
			_direction = value;
		}

		public function get triggerTime():int
		{
			return _triggerTime;
		}

		public function get matchHash2():int
		{
			return _matchHash2;
		}

		public function get matchHash1():int
		{
			return _matchHash1;
		}

		public function get nothingElse():Boolean
		{
			return _nothingElse;
		}

		public function get heldDown():Boolean
		{
			return _heldDown;
		}
		
		public function get triggerOnRelease():Boolean 
		{
			return this._triggerTime != -1;
		}
		
		public function buildHash1():int
		{
			var hash:int = 0;
			switch (this._direction)
			{
				case CommandDirection.B4Way:
				case CommandDirection.B:
					hash |= PlayerButton.Left;
					break;
				
				case CommandDirection.DB:
					hash |= PlayerButton.Down;
					hash |= PlayerButton.Left;
					break;
				
				case CommandDirection.D4Way:
				case CommandDirection.D:
					hash |= PlayerButton.Down;
					break;
				
				case CommandDirection.DF:
					hash |= PlayerButton.Down;
					hash |= PlayerButton.Right;
					break;
				
				case CommandDirection.F4Way:
				case CommandDirection.F:
					hash |= PlayerButton.Right;
					break;
				
				case CommandDirection.UF:
					hash |= PlayerButton.Up;
					hash |= PlayerButton.Right;
					break;
				
				case CommandDirection.U4Way:
				case CommandDirection.U:
					hash |= PlayerButton.Up;
					break;
				
				case CommandDirection.UB:
					hash |= PlayerButton.Up;
					hash |= PlayerButton.Left;
					break;
				
				case CommandDirection.None:
				default:
					break;
			}
			
			if ((this._button & CommandButton.A) == CommandButton.A) hash |= PlayerButton.A;
			if ((this._button & CommandButton.B) == CommandButton.B) hash |= PlayerButton.B;
			if ((this._button & CommandButton.C) == CommandButton.C) hash |= PlayerButton.C;
			if ((this._button & CommandButton.X) == CommandButton.X) hash |= PlayerButton.X;
			if ((this._button & CommandButton.Y) == CommandButton.Y) hash |= PlayerButton.Y;
			if ((this._button & CommandButton.Z) == CommandButton.Z) hash |= PlayerButton.Z;
			if ((this._button & CommandButton.Taunt) == CommandButton.Taunt) hash |= PlayerButton.Taunt;
			
			return hash;
			
		}
		
		public function buildHash2():int
		{
			var hash:int = 0;
			
			switch (this._direction)
			{
				case CommandDirection.B:
					hash |= PlayerButton.Up;
					hash |= PlayerButton.Down;
					hash |= PlayerButton.Right;
					break;
				
				case CommandDirection.DB:
					hash |= PlayerButton.Up;
					hash |= PlayerButton.Right;
					break;
				
				case CommandDirection.D:
					hash |= PlayerButton.Up;
					hash |= PlayerButton.Left;
					hash |= PlayerButton.Right;
					break;
				
				case CommandDirection.DF:
					hash |= PlayerButton.Up;
					hash |= PlayerButton.Left;
					break;
				
				case CommandDirection.F:
					hash |= PlayerButton.Up;
					hash |= PlayerButton.Down;
					hash |= PlayerButton.Left;
					break;
				
				case CommandDirection.UF:
					hash |= PlayerButton.Down;
					hash |= PlayerButton.Left;
					break;
				
				case CommandDirection.U:
					hash |= PlayerButton.Left;
					hash |= PlayerButton.Down;
					hash |= PlayerButton.Right;
					break;
				
				case CommandDirection.UB:
					hash |= PlayerButton.Down;
					hash |= PlayerButton.Right;
					break;
				
				case CommandDirection.B4Way:
				case CommandDirection.U4Way:
				case CommandDirection.F4Way:
				case CommandDirection.D4Way:
				case CommandDirection.None:
				default:
					break;
			}
			
			return hash;
		}
	}
}